<template>
  <div class="home">
    <div class="home-content">
      <div class="home-top-title">
        <img src="@/assets/images/asset.png" alt="">
        <div>导入的钱包</div>
      </div>
      <div class="table">
        <div v-if="istable">
          <el-table
            :data="tableData"
            @row-click="detail"
            style="width: 100%">
            <el-table-column
              prop=""
              label=""
              width="30">
            </el-table-column>
            <el-table-column
              prop="type"
              :label="this.$t('h.walletName')"
              width="250">
              <template slot-scope="scope">
                <div class="wallet">
                  <div v-if="scope.row.type==='ETH'"><img src="@/assets/images/ethUnit.png" alt=""></div>
                  <div v-if="scope.row.type==='BTC'"><img src="@/assets/images/btcUnit.png" alt=""></div>
                  <div v-if="scope.row.type==='EOS'"><img src="@/assets/images/eosUnit.png" alt=""></div>
                  <div>{{scope.row.type}}</div>
                </div>
              </template>
            </el-table-column>
            <el-table-column
              prop="address"
              :label="this.$t('h.address')"
              width="400">
            </el-table-column>
            <el-table-column
              prop="balance"
              :label="this.$t('h.balance')">
              <template slot-scope="scope">
                <span>{{scope.row.balance}}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="value"
              :label="this.$t('h.value')">
              <template slot-scope="scope">
                <span>{{scope.row.value}} USD</span>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div v-else class="none">{{$t("h.nodata")}}</div>
        <div class="hand-wallet">
          <div class="lead" @click="importWallethandle"><img src="@/assets/images/icon-import.png" alt="">{{$t("h.importWallet")}}</div>
        </div>
      </div>

    </div>
    <import-outwallet v-if="showwallet" @showHand="showHand"></import-outwallet>
    <import-eth v-if="showwalleteth" @listenImporteth="listenImporteth"></import-eth>
    <success-import v-if="showSuccess" @successListen="successListen"></success-import>
    <fail-import v-if="showFail" @failListen="failListen"></fail-import>
  </div>
</template>
<script>
  import importOutwallet from '@/components/importOutwallet'
  import importEth from '@/components/importEth'
  import successImport from '@/components/successImport'
  import failImport from '@/components/failImport'
  import {getAllOuterAccount} from '../../service/datxService'
  import {getBalance} from '../../service/ethService'
  import {getPrice} from '../../utils/commonUtils'
  import Async from 'async'
  import _ from 'lodash'

  export default {
    components: {
      importOutwallet,
      importEth,
      successImport,
      failImport
    },
    data() {
      return {
        showwallet: false,
        showwalleteth: false,
        showSuccess: false,
        showFail: false,
        istable: true,
        tableData: []
      }
    },
    created() {
      this.getAllout()
    },
    methods: {
      // 获取外部账号
      getAllout() {
        let self = this;
        let obj = getAllOuterAccount();
        if (obj == null) {
          this.istable = false;
        } else {
          this.tableData = obj
        }
        let table = [];
        Async.each(this.tableData, function (obj, callback) {
          obj.value = '';
          obj.balance = '';
          Async.series({
            balance: function (callback) {
              getBalance(obj.address, function (err, res) {
                callback(err, res)
              })
            },
            value: function (callback) {
              getPrice(function (err, res) {
                callback(err, res)
              })
            }
          }, function (err, result) {
            obj.balance = result.balance;
            obj.value = parseFloat(result.balance) * parseFloat(result.value.USD);
            table.push(obj);
            callback(null, null)
          })
        }, function (err) {
          if (err)
            console.log(err);
          self.tableData = table;
        })
      },
      // 打开导入钱包弹框
      importWallethandle() {
        this.showwallet = true;
      },
      // 第一步导入钱包选择
      showHand(data, state) {
        this.showwallet = data;
        if (state === 'open') {
          this.showwalleteth = !data
        }
      },
      // 第二步Eth导入弹框
      listenImporteth(data, state) {
        this.showwalleteth = data;
        if (state === 'success') {
          this.showSuccess = !data;
        } else if (state === 'fail') {
          this.showFail = !data;
        }
      },
      // 第三步成功
      successListen(data) {
        this.showSuccess = data
        this.$router.go(0)
      },
      // 第三步失败
      failListen(data) {
        this.showFail = data
      },
      detail(row) {
        if (row.type === 'ETH') {
          this.$router.push({path: '/account/ETH'});
        }
        if (row.type === 'BTC') {
          this.$router.push({path: '/account/BTC'});
        }
        window.sessionStorage["ethWalletDetail"] = JSON.stringify(row);
      }
    }
  }
</script>
<style lang="less" scoped>
  .home {
    padding-bottom: 40px;
    .home-title {
      color: #000000;
      opacity: 0.6;
      font-size: 30px;
      padding: 25px 0;
    }
    &-content {
      background: #fff;
      min-height: 300px;
      padding-bottom: 20px;
      .home-top-title {
        display: flex;
        align-items: center;
        padding: 30px;
        img {
          width: 34px;
          height: 34px;
        }
        div {
          font-size: 24px;
          padding-left: 8px;
        }
      }
    }
  }

  .none {
    text-align: center;
    margin: 40px 0;
    font-size: 16px;
  }

  .table {
    .none {
      text-align: center;
      margin: 40px 0;
      font-size: 16px;
    }
    .wallet {
      display: flex;
      align-items: center;
      img {
        width: 28px;
        height: 28px;
        margin-right: 5px;
      }
    }
    .asset {
      div {
        span {
          color: #c5c7cb;
        }
      }
    }
    .hand-wallet {
      display: flex;
      justify-content: center;
      padding-top: 30px;
      margin: 0 auto;
      .lead {
        width: 90%;
        height: 70px;
        // opacity: 0.56;
        border-radius: 4px;
        border: dashed 1px #979797;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 20px;
        color: #98889b;
        cursor: pointer;
        img {
          width: 40px;
          height: 40px;
          margin-right: 20px;
        }
      }
      div:last-child {
        margin-right: 0;
      }
    }
  }
</style>
<style lang='less'>
  .table {
    .el-table__body-wrapper {
      tr {
        height: 135px !important;
      }
    }
  }
</style>


